layui.use(['treeTable'], function () {
        var treeTable = layui.treeTable;
        
        // 渲染树形表格
        var insTb = treeTable.render({
            elem: '#tree-table',
            url: ctx+'/sys/dept/list',
            method: "post",
            tree: {
                iconIndex: 0,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'id',  // id字段名称
                pidName: 'pid',    // pid字段名称
                openName: 'deptName',   // 自定义默认展开的字段名
            },
            cols: [[
            	  {field:'deptName', title:'名称', minWidth:150}
                 ,{field:'deptCode', title:'机构编码', minWidth:150}
                 ,{field:'deptType', title:'机构类型', minWidth:150
                	 ,templet: function(d){
                  		  return transDict("deptType",d.deptType);
                  	   }	 
                 }
                 ,{field:'sort', title:'排序', minWidth:80}
                 ,{field:'remark', title:'备注', minWidth:150}
                 ,{
         			title: '操作',
         			align: 'center',
         			width: 180,
         			toolbar: '#dept-t-tool',
         			fixed: 'right'
         		}
            ]],
            parseData:  function(res) { 
    			return {
    				"code" : (res.code = 200 ? 0 : -1), 
    				"msg" : res.msg, 
    				"data" : res.data 
    			};
            }
       
        });

        // 搜索
        $('#deptSearch').click(function () {
        	var searchData = $("#deptSearchForm").serializeObject();
            insTb.reload({where:searchData});
        });

    // 全部展开
    $('#btnExpandAll').click(function () {
        insTb.expandAll();
    });

    // 全部折叠
    $('#btnFoldAll').click(function () {
        insTb.foldAll();
    });
	
	//新增页面
	$("#deptAdd").click(function(){
		var title = $(this).text();
		var url = ctx+"/sys/dept/addView";
		$.get(url, {}, function(res){
			if(!isJSON(res)){
			  var index = layer.open({
					type: 1,
					title: title,
					skin: 'layui-layer-rim', 
					area: ['35%', '65%'], 
					offset: ['5%', '20%'],
			        content: res,
					btn: [],
					end: function(){
						pageReload();
					}
			        ,maxmin: true
			  });
			  layer.full(index);
			}
		  });
	})

	//监听行工具
	treeTable.on('tool(tree-table)', function (obj) {
            var event = obj.event;
            if (event === 'del') {
            	var id = obj.data.id
      	      layer.confirm('确定删除吗？', function(index){
      	    	  ajaxCommit({
      	    		  url : ctx+"/sys/dept/del?id="+id
      	    		  ,successfunc:function(){ pageReload() }
      	    	  })
      	      });
            } else if (event === 'edit') {
            	var title = $(this).text();
        		var id = obj.data.id
        		var url = ctx+"/sys/dept/editView?id="+id;
        		$.get(url, {}, function(res){
        			if(!isJSON(res)){
        			  var index = layer.open({
        					type: 1,
        					title: title,
        					skin: 'layui-layer-rim', 
        					area: ['35%', '65%'], 
        					offset: ['5%', '20%'],
        			        content: res,
        					btn: [],
        					end: function(){
        						pageReload();
        					}
        			  		,maxmin: true
        			  });
        			  layer.full(index);
        			}
        		  });
            }
    });
	
})
				
